package com.czkj.hwz.decorate.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.czkj.hwz.decorate.Activity.DetailActivity;
import com.czkj.hwz.decorate.R;
import com.czkj.hwz.decorate.adapter.ProductSearchAdapter;
import com.czkj.hwz.decorate.base.HttpUrl;
import com.czkj.hwz.decorate.bean.Goods;
import com.google.gson.Gson;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import cn.finalteam.okhttpfinal.HttpCycleContext;
import cn.finalteam.okhttpfinal.HttpRequest;
import cn.finalteam.okhttpfinal.JsonHttpRequestCallback;
import cn.finalteam.okhttpfinal.RequestParams;
import okhttp3.Headers;

/**
 * 作者：lxm on 2017/11/2 20:10
 * 邮箱：18014168291@189.cn
 * 才智科技
 */
public class BaseOperationCenterFragment extends Fragment implements HttpCycleContext {
    private static final String TAG = "BaseOperationCenterFrag";
    protected final String HTTP_TASK_KEY = "HttpTaskKey_" + hashCode();
    private List<Goods> mGoodsList;
    private ListView lv;
    private ProductSearchAdapter productSearchAdapter;
    protected Context mContext;
    private int enterpriseid;//运营中心编号
    private String searchText;//关键字
    private String mTitle;//标题
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:

                    productSearchAdapter.notifyDataSetChanged();

                    break;
            }
        }
    };
    public static BaseOperationCenterFragment newInstance(int enterpriseid,String searchText,String mTitle) {
        Bundle args = new Bundle();
        args.putInt("enterpriseid",enterpriseid);
        args.putString("searchText",searchText);
        args.putString("mTitle",mTitle);
        BaseOperationCenterFragment fragment = new BaseOperationCenterFragment();
        fragment.setArguments(args);
        return fragment;
    }
    public void updateArguments(int enterpriseid,String searchText,String mTitle) {
        this.enterpriseid=enterpriseid;
        this.searchText=searchText;
        this.mTitle=mTitle;
        Bundle args=getArguments();
        if (args!=null) {
            args.putInt("enterpriseid",enterpriseid);
            args.putString("searchText",searchText);
            args.putString("mTitle",mTitle);
        }
    }
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.product_search_fragment,container,false);
        lv= (ListView) view.findViewById(R.id.lv);
        getProductData(enterpriseid,searchText);

        mGoodsList=new ArrayList<>();
        productSearchAdapter=new ProductSearchAdapter(mContext,mGoodsList);
        lv.setAdapter(productSearchAdapter);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getActivity(), DetailActivity.class);
                intent.putExtra("mTitle",mTitle);
                intent.putExtra("mGoodsList", (Serializable) mGoodsList);
                intent.putExtra("position", position);
                startActivity(intent);
            }
        });

        return view;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext=getContext();
        Bundle args=getArguments();
        if (args!=null) {
            enterpriseid=args.getInt("enterpriseid");
            searchText=args.getString("searchText");
            Log.d(TAG, "onCreate: "+enterpriseid);
            Log.d(TAG, "onCreate: "+searchText);

        }
    }
    private void getProductData(int enterpriseid,String searchText) {
        RequestParams params=new RequestParams(this);
        params.addFormDataPart("enterpriseid",String.valueOf(enterpriseid));
        if (TextUtils.isEmpty(searchText)) {

        }else {
            params.addFormDataPart("cmdtype","search");
            params.addFormDataPart("keyword",searchText);
        }
        HttpRequest.get(HttpUrl.GOODS_GET,params,new JsonHttpRequestCallback(){
                    @Override
                    protected void onSuccess(JSONObject jsonObject) {
                        super.onSuccess(jsonObject);
                        parseProductData(jsonObject);
                        Log.d(TAG, TAG+jsonObject.toJSONString());
                    }

                    @Override
                    public void onFailure(int errorCode, String msg) {
                        super.onFailure(errorCode, msg);
                    }

                    @Override
                    public void onResponse(String response, Headers headers) {
                        super.onResponse(response, headers);
                    }
                }
        );
    }
    private void parseProductData(JSONObject jsonObject) {
        mGoodsList.clear();
        JSONArray jsonArray=jsonObject.getJSONArray("rows");
        Gson gson=new Gson();
        for (int i=0;i<jsonArray.size();i++) {
            JSONObject jsonObject1=jsonArray.getJSONObject(i);
            Goods goods=gson.fromJson(jsonObject1.toJSONString(),Goods.class);
            mGoodsList.add(goods);
        }
        handler.sendEmptyMessage(0);

    }

    @Override
    public String getHttpTaskKey() {
        return HTTP_TASK_KEY;
    }
}
